﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OrionApplication.OrionLib.Dao;
using OrionApplication.OrionLib.Orion;
using OrionApplication.OrionLib;

namespace OrionApplication.OrionForm
{
    public partial class ArticleForm :  OrionBaseDoubleForm
    {
        public ArticleForm() 
            : base()
        {
            dao = new DaoArticle();
            InitializeComponent();
        }

        public ArticleForm(string titre)
            : base(titre)
        {
            dao = new DaoArticle();
            InitializeComponent();
        }

        private void DupliqueEnregistrement()
        {
            if (kDgv.SelectedRows.Count > 0)
            {
                if (OrionOutils.ChoixOuiNon(OrionConfBase.ConfirmDuplique) == DialogResult.Yes)
                {
                    Article a = ((DaoArticle)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                    a = ((DaoArticle)dao).Insert(a);
                    this.RempliKDgv();
                    this.FocusSur(a.OrionId);
                }
            }
            else
                OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
        }

        private void RempliForm(Article a)
        {
            kTbGenCode.Text = a.CodeBarre;
            kTbLibelle.Text = a.Libelle;
            kTbRef.Text = a.Reference;
            kTbCommentaire.Text = a.Commentaire;
            kNudPrixAchat.Value = Convert.ToDecimal(a.PrixAchat);
            kNudPrixVente.Value = Convert.ToDecimal(a.PrixVente);
        }

        private void SauvegardeForm()
        {
            Article a = this.RempliObject();
            if (kLblMode.Text == OrionConfBase.OrionModeInsert)
                a = ((DaoArticle)dao).Insert(a);
            else
            {
                if (kDgv.SelectedRows.Count > 0)
                {
                    a.OrionId = Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value);
                    a = ((DaoArticle)dao).Update(a);

                }
                else
                    OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
            }
            this.RempliKDgv();
            this.FocusSur(a.OrionId);
        }

        private Article RempliObject()
        {
            Article a = new Article();
            a.CodeBarre = kTbGenCode.Text;
            a.Libelle = kTbLibelle.Text;
            a.Reference = kTbRef.Text;
            a.Commentaire = kTbCommentaire.Text;
            a.PrixAchat = Convert.ToDouble(kNudPrixAchat.Value);
            a.PrixVente = Convert.ToDouble(kNudPrixVente.Value);
            return a;
        }

        public void RowEnter()
        {
            if (kDgv.SelectedRows.Count > 0)
            {
                Article a = ((DaoArticle)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                this.RempliForm(a);
            }
        }
    }
}
